package com.ruoyi.web.controller.order;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.order.domain.MesOrder;
import com.ruoyi.order.domain.MesWarehousePosition;
import com.ruoyi.order.service.IMesOrderService;
import com.ruoyi.order.service.IMesWarehousePositionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检测订单管理
 * 
 * @author ruoyi
 * @date 2025-01-14
 */
@RestController
@RequestMapping("/order/order")
public class MesOrderController extends BaseController
{
    @Autowired
    private IMesOrderService mesOrderService;

    @Autowired
    private IMesWarehousePositionService mesWarehousePositionService;

    /**
     * 查询检测订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesOrder mesOrder)
    {
        startPage();
        List<MesOrder> list = mesOrderService.selectMesOrderList(mesOrder);
        return getDataTable(list);
    }

    /**
     * 导出检测订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesOrder mesOrder)
    {
        List<MesOrder> list = mesOrderService.selectMesOrderList(mesOrder);
        ExcelUtil<MesOrder> util = new ExcelUtil<MesOrder>(MesOrder.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 查询检测订单详情
     */
    @PreAuthorize("@ss.hasPermi('order:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        MesOrder mesOrder = mesOrderService.selectMesOrderById(id);
        ajax.put(AjaxResult.DATA_TAG, mesOrder);
        MesWarehousePosition mesWarehousePosition = new MesWarehousePosition();
        //查询中转库的空闲库位用于入库
        mesWarehousePosition.setWarehouseId(2L);
        mesWarehousePosition.setStatus("Y");
        if(StringUtils.isNotEmpty(mesOrder.getPositionCode())){
            List<String> positionCodes = StringUtils.str2List(mesOrder.getPositionCode(), ",", true, true);
            ajax.put("positionCodes", positionCodes);
        }
        List<MesWarehousePosition> list =  mesWarehousePositionService.selectMesWarehousePositionList(mesWarehousePosition);
        ajax.put("positions",list);
        return ajax;
    }
    /**
     * 修改检测订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesOrder mesOrder)
    {
        return toAjax(mesOrderService.updateMesOrder(mesOrder));
    }


    /**
     * 删除检测订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesOrderService.deleteMesOrderByIds(ids));
    }
}
